package com.woniu108.product.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.dto.ProductBrandDto;
import com.woniu108.carService.commos.dto.UserInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.produnt.ProductException;
import com.woniu108.carService.commos.exception.produnt.ProductExceptionCode;
import com.woniu108.carService.commos.util.StaticData;
import com.woniu108.product.param.*;
import org.springframework.stereotype.Service;
import com.woniu108.product.client.PersonClient;
import com.woniu108.product.mapper.ProductBrandMapper;
import com.woniu108.product.mapper.ProductInfoMapper;
import com.woniu108.product.mapper.ProductItemMapper;
import com.woniu108.product.mapper.ProductTypeMapper;
import com.woniu108.product.model.ProductBrand;
import com.woniu108.product.model.ProductInfo;
import com.woniu108.product.model.ProductItem;
import com.woniu108.product.model.ProductType;
import com.woniu108.product.service.ProductBrandService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品品牌表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class ProductBrandServiceImpl extends ServiceImpl<ProductBrandMapper, ProductBrand> implements ProductBrandService {
    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private PersonClient personClient;

    @Resource
    private ProductItemMapper productItemMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;
    @Resource
    private ProductBrandMapper productBrandMapper;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
        if (adminInfo.getAdminRoleId() != 1&&adminInfo.getAdminRoleId() != 2) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }


    /**
     * 判断用户
     */
    private void isUser(String userId) {
        // 检查 userId 是否为 null
        if (userId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断用户
        UserInfoDto userInfo = personClient.getByUserId(userId).getData();
        if (userInfo == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_HAVE);
        }
    }
    @Override
    public List<ProductBrandDto> findAllProductBrand(String adminId) {
        //判断是否为管理员
        isAdmin(adminId);
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<>();
        wrapper.ne("p_b_state", StaticData.PRODUCT_BRAND_STATE_DELETE);
        List<ProductBrand> list = productBrandMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, ProductBrandDto.class);

    }

    @Override
    public List<ProductBrandDto> findAllNormalProductBrand() {
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<>();
        wrapper.ne("p_b_state", StaticData.PRODUCT_BRAND_STATE_NORMAL);
        List<ProductBrand> list = productBrandMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, ProductBrandDto.class);

    }

    @Override
    public Boolean insertProductBrand(InsertProductBrandParam param) {
        //判断是否为管理员
        isAdmin(param.getAdminId());
        //查询该种类名是否存在
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("p_b_name", param.getPBName());
        ProductBrand selectOne = productBrandMapper.selectOne(wrapper);
        //判断存不存在
        if (selectOne != null) {
            //存在状态正常或禁用就报错
            if (Objects.equals(selectOne.getPBState(), StaticData.PRODUCT_BRAND_STATE_BAN) || Objects.equals(selectOne.getPBState(), StaticData.PRODUCT_BRAND_STATE_NORMAL)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_HAVE);
            }
            //删除状态就改为正常
            selectOne.setPBState(StaticData.PRODUCT_BRAND_STATE_NORMAL);
            int update = productBrandMapper.updateById(selectOne);
            return update > 0;
        }
        //不存在就新增
        ProductBrand productBrand = new ProductBrand();
        productBrand.setPBName(param.getPBName());
        productBrand.setPBState(StaticData.PRODUCT_BRAND_STATE_NORMAL);
        int insert = productBrandMapper.insert(productBrand);
        return insert > 0;
    }

    @Override
    public Boolean updateProductBrand(UpdateProductBrandParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        //通过id查品牌
        ProductBrand select = productBrandMapper.selectById(param.getPBId());
        if (select == null || Objects.equals(select.getPBState(), StaticData.PRODUCT_BRAND_STATE_DELETE)) {
            throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_NOT_HAVE);
        }
        //通过名字查，看新名字是否同名
        QueryWrapper<ProductBrand> wrapper = new QueryWrapper<>();
        wrapper.eq("p_b_name", param.getPBName());
        ProductBrand selectOne = productBrandMapper.selectOne(wrapper);
        if (selectOne != null) {
            throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_HAVE);
        }
        //修改名字
        select.setPBName(param.getPBName());
        int i = productBrandMapper.updateById(select);
        return i > 0;
    }

    @Override
    public Boolean banProductBrand(BanProductBrandParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        for (Integer pBId : param.getPBIds()) {
            //禁用品牌
            ProductBrand productBrand = productBrandMapper.selectById(pBId);
            //判断是否存在和状态
            if (productBrand == null || Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_BRAND_STATE_DELETE)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_NOT_HAVE);
            }
            if (Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_BRAND_STATE_BAN)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_BANED);
            }
            //禁用
            productBrand.setPBState(StaticData.PRODUCT_BRAND_STATE_BAN);
            productBrandMapper.updateById(productBrand);
            //禁用商品总览
            QueryWrapper<ProductItem> itemQueryWrapper = new QueryWrapper<>();
            itemQueryWrapper.eq("p_b_id", pBId).eq("pro_state",StaticData.PRODUCT_ITEM_STATE_NORMAL);
            List<ProductItem> items = productItemMapper.selectList(itemQueryWrapper);
            //是否存在该种类商品
            if (items != null) {
                for (ProductItem item : items) {
                    //判断item的状态
                    if (item.getProState().equals(StaticData.PRODUCT_ITEM_STATE_NORMAL)) {
                        //禁用
                        item.setProState(StaticData.PRODUCT_ITEM_STATE_BAN);
                        productItemMapper.updateById(item);
                        //查询是否存在商品详情
                        QueryWrapper<ProductInfo> infoQueryWrapper = new QueryWrapper<>();
                        infoQueryWrapper.eq("p_item_id", item.getPItemId()).eq("p_info_state", StaticData.PRODUCT_INFO_STATE_NORMAL);
                        List<ProductInfo> infos = productInfoMapper.selectList(infoQueryWrapper);
                       //判断infos
                        if (infos != null) {
                            //禁用商品详情
                            for (ProductInfo info : infos) {
                                //判断info状态
                                if (info.getPInfoState().equals(StaticData.PRODUCT_INFO_STATE_NORMAL)) {
                                    //禁用
                                    info.setPInfoState(StaticData.PRODUCT_INFO_STATE_BAN);
                                    productInfoMapper.updateById(info);
                                }
                            }
                        }
                    }
                }
            }
        }
        return true;
    }

    @Override
    public Boolean unlockProductBrand(UnlockProductBrandParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        for (Integer pBId : param.getPBIds()) {
            //解锁品牌
            ProductBrand productBrand = productBrandMapper.selectById(pBId);
            //判断是否存在和状态
            if (productBrand == null || Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_BRAND_STATE_DELETE)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_NOT_HAVE);
            }
            if (Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_BRAND_STATE_NORMAL)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_STATE_NORMAL);
            }
            //解锁
            productBrand.setPBState(StaticData.PRODUCT_BRAND_STATE_NORMAL);
            productBrandMapper.updateById(productBrand);
            //解锁商品总览
            QueryWrapper<ProductItem> itemQueryWrapper = new QueryWrapper<>();
            itemQueryWrapper.eq("p_b_id", pBId);
            List<ProductItem> items = productItemMapper.selectList(itemQueryWrapper);
            //是否存在该种类商品
            if (items != null) {
                for (ProductItem item : items) {
                    //判断item的状态
                    if (Objects.equals(item.getProState(), StaticData.PRODUCT_ITEM_STATE_BAN)) {
                        //看种类状态
                        ProductType productType = productTypeMapper.selectById(item.getPTId());
                        if (Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_TYPE_STATE_NORMAL)) {
                            //解锁
                            item.setProState(StaticData.PRODUCT_ITEM_STATE_NORMAL);
                            productItemMapper.updateById(item);
                            //查询是否存在商品详情
                            QueryWrapper<ProductInfo> infoQueryWrapper = new QueryWrapper<>();
                            infoQueryWrapper.eq("p_item_id", item.getPItemId()).eq("p_info_state", StaticData.PRODUCT_INFO_STATE_BAN);
                            List<ProductInfo> infos = productInfoMapper.selectList(infoQueryWrapper);
                            if (infos != null) {
                                //解锁商品详情
                                for (ProductInfo info : infos) {
                                    //判断info状态
                                    if (info.getPInfoState().equals(StaticData.PRODUCT_INFO_STATE_BAN)) {
                                        //解锁
                                        info.setPInfoState(StaticData.PRODUCT_INFO_STATE_NORMAL);
                                        productInfoMapper.updateById(info);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return true;
    }

    @Override
    public Boolean deleteProductBrand(DeleteProductBrandParam param) {
        //判断管理员
        isAdmin(param.getAdminId());
        for (Integer pBId : param.getPBIds()) {
            //删除品牌
            ProductBrand productBrand = productBrandMapper.selectById(pBId);
            //判断是否存在和状态
            if (productBrand == null || Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_BRAND_STATE_DELETE)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_NOT_HAVE);
            }
            if (Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_BRAND_STATE_NORMAL)) {
                throw new ProductException(ProductExceptionCode.PRODUCT_BRAND_STATE_NORMAL);
            }
            //删除
            productBrand.setPBState(StaticData.PRODUCT_BRAND_STATE_DELETE);
            productBrandMapper.updateById(productBrand);
            //删除商品总览
            QueryWrapper<ProductItem> itemQueryWrapper = new QueryWrapper<>();
            itemQueryWrapper.eq("p_b_id", pBId);
            List<ProductItem> items = productItemMapper.selectList(itemQueryWrapper);
            //是否存在该种类商品
            if (items != null) {
                for (ProductItem item : items) {
                    //判断item的状态
                    if (!Objects.equals(item.getProState(), StaticData.PRODUCT_ITEM_STATE_DELETE)) {
                        //看种类状态
                        ProductType productType = productTypeMapper.selectById(item.getPTId());
                        if (!Objects.equals(productBrand.getPBState(), StaticData.PRODUCT_TYPE_STATE_DELETE)) {
                            //删除
                            item.setProState(StaticData.PRODUCT_ITEM_STATE_DELETE);
                            productItemMapper.updateById(item);
                            //查询是否存在商品详情
                            QueryWrapper<ProductInfo> infoQueryWrapper = new QueryWrapper<>();
                            infoQueryWrapper.eq("p_item_id", item.getPItemId()).eq("p_info_state", StaticData.PRODUCT_INFO_STATE_BAN);
                            List<ProductInfo> infos = productInfoMapper.selectList(infoQueryWrapper);
                            if (infos != null) {
                                //解锁商品详情
                                for (ProductInfo info : infos) {
                                    //判断info状态
                                    if (Objects.equals(info.getPInfoState(), StaticData.PRODUCT_INFO_STATE_BAN)) {
                                        //解锁
                                        info.setPInfoState(StaticData.PRODUCT_INFO_STATE_NORMAL);
                                        productInfoMapper.updateById(info);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
}
